﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Site.Models;

namespace Site.Controllers
{
    public class StatsController : Controller
    {
        private BMSiteDBEntities _db = new BMSiteDBEntities();
        private IStatisticRepository repository;

        public StatsController()
            : this(new StatisticRepository())
        {
        }


        public StatsController(IStatisticRepository rep)
        {
            // TODO: Complete member initialization
            repository = rep;
        }
        //
        // GET: /Stats/

        public ActionResult IndexBody(string GameTypeId, string StatTypeId)
        {

            // StatTypeId == "1" pour une selection de stat utilisateur
            if (StatTypeId == "1")
            {
                ViewData["users"] = repository.GetUsersStats(int.Parse(GameTypeId)).OrderByDescending(s => s.UserName);
            }
            // StatTypeId == "2" pour une selection de stat Clan
            else if (StatTypeId == "2")
            {
                ViewData["clans"] = repository.GetClansStats(int.Parse(GameTypeId));
            }
            else
            {
                ViewData["users"] = repository.GetUsersStats(int.Parse(GameTypeId));
                ViewData["clans"] = repository.GetClansStats(int.Parse(GameTypeId));
            }
                  
            return PartialView(ViewData);
        }
        public ActionResult Index()
        {
            ViewData["users"] = repository.GetUsersStats(1);
            ViewData["clans"] = repository.GetClansStats(1);
            //ViewData["roles"] = repository.GetGameTypes();
            ViewData["GameTypeId"] = repository.GetGameTypesDDL();
            ViewData["StatTypeId"] = repository.GetStatTypeDDL();

            return View(ViewData);
        }


        /*  public PartialViewResult UsersStat()
          {
              var userIds = (from u in _db.STATISTIC
                                 select u.STAT_USER_ID).ToList();

              List<Statistic> model = new List<Statistic>();
              foreach (Guid m in userIds)
              {
                  model.Add(new Statistic
                  {

                      users = _db.aspnet_Users.First(p => p.UserId == m),

                      stats = _db.STATISTIC.First(p => p.STAT_USER_ID == m)
                  });


              }
              return PartialView(model);
          }
         */
        /*public PartialViewResult ClansStat()
        {
            var clanIds = (from u in _db.STATISTIC
                           select u.STAT_CLAN_ID).ToList();

            List<Statistic> model = new List<Statistic>();
            foreach (Guid m in clanIds)
            {
                model.Add(new Statistic
                {

                    clans = _db.CLAN.First(p => p.CLAN_ID == m),

                    stats = _db.STATISTIC.First(p => p.STAT_USER_ID == m)
                });


            }
            return PartialView(model);
        }
        */
        //
        // GET: /Stats/Details/5

        public ActionResult Details(Guid id)
        {

            return View(repository.GetUserDetailedStats(id));
        }

        //
        // GET: /Stats/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Stats/Create

        [HttpPost]
        public ActionResult Reset(Guid id)
        {
       
                // TODO: Add insert logic here
                repository.ResetStat(id);
                return RedirectToAction("Index");
  
        }

       
    }
}
